package com.brave.manager.dao;

import com.brave.manager.domian.Student;

public class StudentDao {
    private static Student[] Students = new Student[5];

    static {
        Student stu1 = new Student("001", "胖鸡", "18", "男");
        Student stu2 = new Student("002", "BraveY", "19", "男");
        Students[0] = stu1;
        Students[1] = stu2;
    }

    public Student[] findAllStudent() {
        return Students;
    }

    public static boolean addStudent(Student stu) {
        int index = -1;
        for (int i = 0; i < Students.length; i++) {
            Student Stu = Students[i];
            if (Stu == null) {
                index = i;
                break;
            }
        }
        if (index == -1) {
            return false;
        } else {
            Students[index] = stu;
            return true;
        }
    }

    public void deleteStudent(String id) {
        int index = getIdex(id);
        Students[index] = null;
    }

    private int getIdex(String id) {
        int index = -1;
        for (int i = 0; i < Students.length; i++) {
            Student stu = Students[i];
            if (stu != null && stu.getId().equals(id)) {
                index = i;
                break;
            }
        }
        return index;
    }

    public void updateStudent(String id, Student stu) {
        int index = getIdex(id);
        Students[index] = stu;
    }
}
